package directory_sync;

import java.util.*;
import java.io.*;

//walks through a directory and generates a sorted file list
public final class SyncReader
{
	//contains all files in the FQDN format
	private List<File> mainFileList; 
	
	public SyncReader(String aDirectory)
	{
		String directory = aDirectory;
		File startDirectory = new File(directory);
		mainFileList = genFileList(startDirectory);
	}
	
	public List<File> genFileList(File files)
	{
		List<File> resultList = null;
		resultList = genFileListRecursive(files);
		//sort the list
		Collections.sort(resultList);
		return resultList;
	}
	
	private List<File> genFileListRecursive(File currentDir)
	{
		List<File> resultList = new ArrayList<File>();
		File[] currentFiles = currentDir.listFiles();
		List<File> currentList = Arrays.asList(currentFiles);
		//read file structure recursive
		for(File file : currentList)
		{
			resultList.add(file);
			//folders only
			if(!file.isFile())
			{
				List<File> deeperList = genFileListRecursive(file);
				resultList.addAll(deeperList);
			}
		}
		return resultList;
	}
	
	public List<File> getFileList()
	{   
		return mainFileList;
	}
} 